package liu.thread.accont;

/**
 * 两个同步线程竞争资源
 * @author My
 *
 */
public class Accont {
	
	private int countera=0;
	private int counterb=0;
	private int cs=0;
	private int c=0;
	private boolean flag=true;
	
	Object obj=new Object();
	
	public synchronized boolean a() throws InterruptedException{
		
			if(countera==3){
				
				System.out.println("a从头开始");
				countera=0;
				flag=false;
				notify();
				wait();
				System.out.println("a从wait开始");
			}
			
			countera++;
			System.out.println(Thread.currentThread().getName()+"储钱"+countera+"次");
			cs++;
			
			//结束
			if(cs==12){
				notify();
				return false;
			}
		
		return true;
	}
	
	
	
	
	public synchronized boolean b() throws InterruptedException{
		
			//切换线程
		synchronized (obj) {
			
		}
			if(counterb==3){
				System.out.println("b从头开始");
				counterb=0;
				flag=true;
				notify();
				wait();
				System.out.println("b从wait开始");
			}
			
			counterb++;
			System.out.println(Thread.currentThread().getName()+"储钱"+counterb+"次");
			c++;
			
			//结束
			if(c==12){
				notify();
				return false;
			}
				return true;
	}

}
